Skip to content

Conversation

@MarekPorwisz
Copy link
Contributor

Implemented stop function that immedtaietly releases all threads waiting for RPC response and automatically fails all new calls. Implemented resume function that restores normal RPC communication. This is intended to be used for recovery process when the other side needs to be reset but there are calls waiting for the response. Additionally it is possible to provide callback handler that could be used to do the custom cleanup, for example clear the callback table.

void nrf_rpc_register_cleanup_handler(struct nrf_rpc_cleanup_handler * handler);


/** @brief Temporairly suspend all RPC communication
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/** @brief Temporairly suspend all RPC communication
/** @brief Temporarily suspend all RPC communication

/** @brief Temporairly suspend all RPC communication
*
* Calling this function automatically fails all communication until @ref nrf_rpc_resume is called.
* It also causes all pending request to fail immediately.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* It also causes all pending request to fail immediately.
* It also causes all pending commands to fail immediately.

*
* @param cleanup set to true to also invoke all the custom cleanup handlers.
*
* @note If the cleanup parameter is set to true it may be needed to reset the RPC server before invoking
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @note If the cleanup parameter is set to true it may be needed to reset the RPC server before invoking
* @note If the cleanup parameter is set to true it may be needed to reset the peer before invoking

nRF RPC has no concept of client/server.
I think this note may be a bit confusing as to why the peer must be reset though. Also the local device may not be capable of resetting the peer. This note might require some explanation for this reason.

#include <stdbool.h>
#include <string.h>

#include <zephyr/kernel.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No Zephyr includes please.

for (int i = 0; i < CONFIG_NRF_RPC_CMD_CTX_POOL_SIZE; i++) {
struct nrf_rpc_cmd_ctx *ctx = &cmd_ctx_pool[i];
nrf_rpc_os_mutex_lock(&ctx->mutex);
if (ctx->recv_msg.waiting > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to have changed the nrf_rpc_os_msg - requiring that it support waiting member. If so then nrf_rpc_os_tmpl.h needs to be udpated and release notes added. I wonder if we couldn't rely on ctx->use_count, instead. The only possibility that use_count > 0 and ctx->mutex is unlocked is because the context owners is waiting for a response.

hdr.type == NRF_RPC_PACKET_TYPE_EVT ||
hdr.type == NRF_RPC_PACKET_TYPE_RSP)) {
// drop only selected types of packets
// do not drop ACKs INITs and ERRORS
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for excluding ACKs as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking it is best to allow acks to consider frame delivered and possibly prevent re-transmissions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, but these are not transport-level ACKs. nRF RPC has ACK packets (not frames) to acknowledge event reception :).

hdr.dst_group_id = group->data->dst_group_id;
header_cmd_encode(full_packet, &hdr);
cmd_ctx = cmd_ctx_reserve();
nrf_rpc_os_mutex_unlock(&cleanup_mutex); /* release the mutex as the context specific one has been aqqured */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
nrf_rpc_os_mutex_unlock(&cleanup_mutex); /* release the mutex as the context specific one has been aqqured */
nrf_rpc_os_mutex_unlock(&cleanup_mutex); /* release the mutex as the context specific one has been acquired */


static void abort_all_ops(void)
{
NRF_RPC_WRN("Canceling all tasks.");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be a warn? It seems to be expected after user's action, how about using info level?

* after the function has finished.
*
*/

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

accidental newline

void (*handler)(void *context);

/** @brief Custom context passed as the function parameter */
void *context;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double space. It seems that compliance script is not running for this repository.

return err;
}

void nrf_rpc_register_cleanup_handler(struct nrf_rpc_cleanup_handler * handler)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
void nrf_rpc_register_cleanup_handler(struct nrf_rpc_cleanup_handler * handler)
void nrf_rpc_register_cleanup_handler(struct nrf_rpc_cleanup_handler *handler)

{
nrf_rpc_os_mutex_lock(&cleanup_mutex);

struct nrf_rpc_cleanup_handler * current;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
struct nrf_rpc_cleanup_handler * current;
struct nrf_rpc_cleanup_handler *current;

is_stopped = true;
abort_all_ops();
if (cleanup) {
struct nrf_rpc_cleanup_handler * current;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
struct nrf_rpc_cleanup_handler * current;
struct nrf_rpc_cleanup_handler *current;

current->handler(current->context);
}
}
nrf_rpc_os_mutex_unlock(&cleanup_mutex);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: maybe newline?

*
*/

void nrf_rpc_register_cleanup_handler(struct nrf_rpc_cleanup_handler * handler);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
void nrf_rpc_register_cleanup_handler(struct nrf_rpc_cleanup_handler * handler);
void nrf_rpc_register_cleanup_handler(struct nrf_rpc_cleanup_handler *handler);

Implemented stop function that immedtaietly releases all threads waiting
for RPC response and automatically fails all new calls.
Implemented resume function that restores normal RPC communication.
This is intended to be used for recovery process when the other side
needs to be reset but there are calls waiting for the response.
Additionally it is possible to provide callback handler that could be
used to do the custom cleanup, for example clear the callback table.

Signed-off-by: Marek Porwisz <[email protected]>
@sonarqubecloud
Copy link

@rlubos rlubos merged commit 545ef5d into nrfconnect:main Oct 24, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants